package com.socool.sinofaith.dao;

import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.socool.sinofaith.model.QuestionAnswerBean;
import com.socool.sinofaith.model.SifSubTaskBean;
import com.socool.sinofaith.model.SifTaskBean;
import com.socool.sinofaith.model.SifTaskQuestionBean;
import com.socool.sinofaith.util.AndroidSQLiteOpenHelper;

public class SifTaskQuestionDao {
	private AndroidSQLiteOpenHelper dbHelper;

	public SifTaskQuestionDao(Context context) {
		dbHelper = new AndroidSQLiteOpenHelper(context);
	}

	// 插入记录
	public int insert(SifTaskQuestionBean sifTaskQuestionBean) {
		Log.i("SQLite", "----insert----");
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.beginTransaction();
		try {
			db.execSQL(
					"insert into "
							+ SifTaskQuestionBean.TABLENAME
							+ " (TaskQuestion_ID,Template_ID,Question,QuestionType,HasOtherOption,OrderIndex,IsRequired,PhotoNum,PhotoMode,PhotoDesc,Task_ID,ZaikuFlag,JumpTo) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
					new Object[] { sifTaskQuestionBean.getTaskQuestion_ID(),
							sifTaskQuestionBean.getTemplate_ID(),
							sifTaskQuestionBean.getQuestion(),
							sifTaskQuestionBean.getQuestionType(),
							sifTaskQuestionBean.getHasOtherOption(),
							sifTaskQuestionBean.getOrderIndex(),
							sifTaskQuestionBean.getIsRequired(),
							sifTaskQuestionBean.getPhotoNum(),
							sifTaskQuestionBean.getPhotoMode(),
							sifTaskQuestionBean.getPhotoDesc(),
							sifTaskQuestionBean.getTask_ID(),
							sifTaskQuestionBean.getZaikuFlag(),
							sifTaskQuestionBean.getJumpTo()});
			db.setTransactionSuccessful();
		} catch (Exception e) {
			return 0;
		} finally {
			db.endTransaction();
		}
		db.close();
		return 1;
	}

	// 删除记录
	public int delete(SifTaskQuestionBean sifTaskQuestionBean) {
		Log.i("SQLite", "----delete----");
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.beginTransaction();
		try {
			db.execSQL("delete from " + SifTaskQuestionBean.TABLENAME
					+ " where TaskQuestion_ID = ?",
					new Object[] { sifTaskQuestionBean.getTaskQuestion_ID() });
			db.setTransactionSuccessful();
		} catch (Exception e) {
			return 0;
		} finally {
			db.endTransaction();
		}
		db.close();
		return 1;
	}

	// 根据taskId删除记录
	public int deleteByTaskId(String taskId) {
		Log.i("SQLite", "----delete----");
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.beginTransaction();
		try {
			db.execSQL("delete from " + SifTaskQuestionBean.TABLENAME
					+ " where Task_ID = ?", new Object[] { taskId });
			db.setTransactionSuccessful();
		} catch (Exception e) {
			return 0;
		} finally {
			db.endTransaction();
		}
		db.close();
		return 1;
	}

	// 更新记录
	public int update(SifTaskQuestionBean sifTaskQuestionBean) {
		Log.i("SQLite", "----update----");
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.beginTransaction();
		try {
			db.execSQL(
					"update "
							+ SifTaskQuestionBean.TABLENAME
							+ " set Template_ID=?, Question=?, QuestionType=?, HasOtherOption=?, OrderIndex=?, IsRequired=?, PhotoNum=?, PhotoMode=?, PhotoDesc=?, Task_ID=?, ZaikuFlag=?, JumpTo=? where TaskQuestion_ID=?",
					new Object[] { sifTaskQuestionBean.getTemplate_ID(),
							sifTaskQuestionBean.getQuestion(),
							sifTaskQuestionBean.getQuestionType(),
							sifTaskQuestionBean.getHasOtherOption(),
							sifTaskQuestionBean.getOrderIndex(),
							sifTaskQuestionBean.getIsRequired(),
							sifTaskQuestionBean.getPhotoNum(),
							sifTaskQuestionBean.getPhotoMode(),
							sifTaskQuestionBean.getPhotoDesc(),
							sifTaskQuestionBean.getTask_ID(),
							sifTaskQuestionBean.getZaikuFlag(),
							sifTaskQuestionBean.getJumpTo(),
							sifTaskQuestionBean.getTaskQuestion_ID() });
			db.setTransactionSuccessful();
		} catch (Exception e) {
			return 0;
		} finally {
			db.endTransaction();
		}
		db.close();
		return 1;
	}

	// 查询记录
	public ArrayList<SifTaskQuestionBean> query(String id) {
		Log.i("SQLite", "----query----");
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor cursor;
		SifTaskQuestionBean sifTaskQuestionBean;
		ArrayList<SifTaskQuestionBean> list = new ArrayList<SifTaskQuestionBean>();
		// 若fileId为null或""则查询所有记录
		if (id == null || id.equals("")) {
			cursor = db.rawQuery("select * from "
					+ SifTaskQuestionBean.TABLENAME, null);
		} else {
			cursor = db.rawQuery("select * from "
					+ SifTaskQuestionBean.TABLENAME
					+ " where TaskQuestion_ID=?", new String[] { id });
		}
		while (cursor.moveToNext()) {
			sifTaskQuestionBean = new SifTaskQuestionBean();
			sifTaskQuestionBean.setTaskQuestion_ID(cursor.getString(cursor
					.getColumnIndex("TaskQuestion_ID")));
			sifTaskQuestionBean.setTemplate_ID(cursor.getString(cursor
					.getColumnIndex("Template_ID")));
			sifTaskQuestionBean.setQuestion(cursor.getString(cursor
					.getColumnIndex("Question")));
			sifTaskQuestionBean.setQuestionType(cursor.getString(cursor
					.getColumnIndex("QuestionType")));
			sifTaskQuestionBean.setHasOtherOption(cursor.getString(cursor
					.getColumnIndex("HasOtherOption")));
			sifTaskQuestionBean.setOrderIndex(cursor.getString(cursor
					.getColumnIndex("OrderIndex")));
			sifTaskQuestionBean.setIsRequired(cursor.getString(cursor
					.getColumnIndex("IsRequired")));
			sifTaskQuestionBean.setPhotoNum(cursor.getString(cursor
					.getColumnIndex("PhotoNum")));
			sifTaskQuestionBean.setPhotoMode(cursor.getString(cursor
					.getColumnIndex("PhotoMode")));
			sifTaskQuestionBean.setPhotoDesc(cursor.getString(cursor
					.getColumnIndex("PhotoDesc")));
			sifTaskQuestionBean.setTask_ID(cursor.getString(cursor
					.getColumnIndex("Task_ID")));
			sifTaskQuestionBean.setZaikuFlag(cursor.getString(cursor
					.getColumnIndex("ZaikuFlag")));
			sifTaskQuestionBean.setJumpTo(cursor.getString(cursor
					.getColumnIndex("JumpTo")));
			Log.i("SQLite", sifTaskQuestionBean.toString());
			list.add(sifTaskQuestionBean);
		}
		cursor.close();
		db.close();
		if (list.size() == 0) {
			Log.i("SQLite", "****表中无数据****");
		}
		return list;
	}

	// 自定义查询记录
	public ArrayList<SifTaskQuestionBean> queryBySql(String sql) {
		Log.i("SQLite", "----queryBySql----");
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor cursor;
		SifTaskQuestionBean sifTaskQuestionBean;
		ArrayList<SifTaskQuestionBean> list = new ArrayList<SifTaskQuestionBean>();
		cursor = db.rawQuery(sql, null);
		while (cursor.moveToNext()) {
			sifTaskQuestionBean = new SifTaskQuestionBean();
			sifTaskQuestionBean.setTaskQuestion_ID(cursor.getString(cursor
					.getColumnIndex("TaskQuestion_ID")));
			sifTaskQuestionBean.setTemplate_ID(cursor.getString(cursor
					.getColumnIndex("Template_ID")));
			sifTaskQuestionBean.setQuestion(cursor.getString(cursor
					.getColumnIndex("Question")));
			sifTaskQuestionBean.setQuestionType(cursor.getString(cursor
					.getColumnIndex("QuestionType")));
			sifTaskQuestionBean.setHasOtherOption(cursor.getString(cursor
					.getColumnIndex("HasOtherOption")));
			sifTaskQuestionBean.setOrderIndex(cursor.getString(cursor
					.getColumnIndex("OrderIndex")));
			sifTaskQuestionBean.setIsRequired(cursor.getString(cursor
					.getColumnIndex("IsRequired")));
			sifTaskQuestionBean.setPhotoNum(cursor.getString(cursor
					.getColumnIndex("PhotoNum")));
			sifTaskQuestionBean.setPhotoMode(cursor.getString(cursor
					.getColumnIndex("PhotoMode")));
			sifTaskQuestionBean.setPhotoDesc(cursor.getString(cursor
					.getColumnIndex("PhotoDesc")));
			sifTaskQuestionBean.setTask_ID(cursor.getString(cursor
					.getColumnIndex("Task_ID")));
			sifTaskQuestionBean.setZaikuFlag(cursor.getString(cursor
					.getColumnIndex("ZaikuFlag")));
			sifTaskQuestionBean.setJumpTo(cursor.getString(cursor
					.getColumnIndex("JumpTo")));
			Log.i("SQLite", sifTaskQuestionBean.toString());
			list.add(sifTaskQuestionBean);
		}
		cursor.close();
		db.close();
		if (list.size() == 0) {
			Log.i("SQLite", "****表中无数据****");
		}
		return list;
	}

	public Long getTaskQuestionCount(String taskId) {
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor cursor;
		String sql = "select  count(*) from " + SifTaskQuestionBean.TABLENAME
				+ " where Task_ID=" + taskId;
		cursor = db.rawQuery(sql, null);
		cursor.moveToFirst();
		Long count = cursor.getLong(0);
		cursor.close();

		return count;
	}
	
	public Long getAllTaskCount(String taskId,String tableName) {
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor cursor;
		String sql = "select  * from " + tableName
				+ " where Task_ID=" + taskId;
		if(tableName.equals(SifSubTaskBean.TABLENAME)){
			sql+=" and HasChild=0";
		}
		cursor = db.rawQuery(sql, null);
		cursor.moveToFirst();
		Long count = (long) cursor.getCount();
		cursor.close();

		return count;
	}

	public Long getSifTaskQuestionCount(String subtaskId) {
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor cursor;
		String sql = "select  count(*) from " + SifTaskQuestionBean.TABLENAME
				+ " where SubTask_ID=" + subtaskId;
		cursor = db.rawQuery(sql, null);
		cursor.moveToFirst();
		Long count = cursor.getLong(0);
		cursor.close();

		return count;
	}

}
